import requests
import re
import os

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/993.0.4577.63 Safari/537.36'
}

def fetch_ips(url):
    response = requests.get(url, headers=headers, timeout=3)
    response.raise_for_status()
    content = response.text

    # 使用正则表达式提取IP地址
    ips = re.findall(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', content)
    return ips

def write_to_file(filename, ips):
    with open(filename, 'w') as f:
        for ip in ips:
            # f.write(f"Require not ip {ip}\n")
            # python 3.5以下调整代码
            f.write("Require not ip {}\n".format(ip))

def main():
    urls = [
        'https://gitee.com/mithrill/BlockingIPAddresses/raw/master/wjwdenyip.txt',
        'http://61.155.106.104:1234/teapot_badip.txt',
        'https://www.binarydefense.com/banlist.txt',
        'http://cinsscore.com/list/ci-badguys.txt',
        'https://report.cs.rutgers.edu/DROP/attackers'
    ]
    filename = '/etc/apache2/denyip2.conf'
    all_ips = []

    for url in urls:
        try:
            ips = fetch_ips(url)
            all_ips.extend(ips)
        except (requests.RequestException, re.error) as e:
            # print(f"Error fetching IP addresses from {url}: {e}")
            print("Error fetching IP addresses from {}: {}".format(url, e))
            continue

    # 去除重复的IP地址
    all_ips = list(set(all_ips))

    # 按照数字从小到大排序
    all_ips.sort(key=lambda ip: [int(x) for x in ip.split('.')])

    try:
        write_to_file(filename, all_ips)
        # print(f"IP addresses written to {filename} successfully.")
        print("IP addresses written to {} successfully.".format(filename))
        os.system("sudo service apache2 graceful")
        print("reload apache2 service.")
        
    except IOError as e:
        # print(f"Error writing to {filename}: {e}")
        print("Error writing to {}: {}".format(url, e))

if __name__ == '__main__':
    main()